<template>
    <div class="icons">
        <swiper :options="swiperOption">
            <swiper-slide v-for="(page,index) in pages" :key="index">
                <div class="icon" v-for="item in page" :key="item.id">
                    <div class="icon-img">
                        <img class="icon-img-content" :src="item.imgUrl">
                    </div>
                    <p class="icon-desc">{{ item.desc }}</p>
                </div>
            </swiper-slide>
        </swiper>
    </div>
</template>

<script>
export default {
    name:'HomeIcons',
    props:['iconslist'],
    data() {
        return {
            swiperOption:{
                autoplay: false  //解决自动轮播的问题
            }
        }
    },
    computed:{
        pages() {
            const pages = []; //最终的分页结果
            // 把一维数组拆分为二位数组，页面和对应的数据项想关联
            this.iconslist.forEach((item,index) => { //遍历iconList
                const page = Math.floor(index/8); //每8条为一页；例：0-7为第一页，page为当前页码
                if(!pages[page]){ // 判断pages中当前页的存储数组是否已声明，未声明则声明当前页为数组格式；
                    pages[page] = [];
                }
                pages[page].push(item); // 将当前icon信息放入对应的页码数组中；
            })
            // 返回分页之后的icon集合；
            return pages;
        }
    }
}
</script>

<style lang="stylus" scoped>
    @import '~styles/varibles.styl'
    @import '~styles/mixins.styl'
    .icons >>> .swiper-container
        height: 0
        padding-bottom: 50%
    .icons
        margin-top: .1rem
        .icon
            position: relative
            overflow: hidden
            width: 25%
            height: 0
            float: left
            padding-bottom: 25% // padding-bottom的百分比的值是按照父元素宽度的值计算
            .icon-img
                position: absolute
                top: 0
                left: 0
                right: 0
                bottom: .44rem
                box-sizing: border-box
                padding: .1rem
                .icon-img-content
                    display: block
                    margin: 0 auto
                    height: 100%
            .icon-desc
                position: absolute
                left: 0
                right: 0
                bottom: 0
                height: .44rem
                line-height: .44rem
                color: $darkTextColor
                text-align: center
                ellipsis()
            
</style>